Ext.define('js.Util', {
    alternateClassName: 'util',
    statics: {
        sysPageSize: 10,
        gridContainerAlign: 'center',
        get: function (url, params = {}, success, fail, scope) {
            return Ext.Ajax.request({
                method: 'GET', url: url, params: params,
                success: success, failure: fail, scope: scope
            });
        },

        post: function (url, params = {}, success, fail, scope) {
            return Ext.Ajax.request({
                method: 'POST', url: url, params: params,
                success: success, failure: fail, scope: scope
            });
        },


        // 密码验证操作
        prompt: function (cb, scope) {
            Ext.create('Ext.window.Window', {
                title: '关键操作密码验证',
                height: '260', width: '500',
                border: false, modal: true, constrainHeader: true, buttonAlign: 'center',
                items: [{
                    padding: '30 30 20 150', html: '<span style="font-size:18px;">请输当前登录用户的密码:</span>', border: false,
                }, {
                    xtype: 'textfield',
                    id: 'authentication',
                    inputType: 'password',
                    padding: '0 30 0 130',
                    autoComplete: false,
                    width: 250,
                }],
                buttons: [{
                    text: '确定',
                    cls:'btnSubmit',
                    scale: 'medium',
                    margin: '0 20 40 0', handler: function () {
                        let value = Ext.getCmp('authentication').getValue(), window = this.up('window');
                        if (value) {
                            js.Util.post('authentication', {password: value})
                                .then(function (response, opts) {
                                        let obj = Ext.decode(response.responseText);
                                        if (obj.success === true) {
                                            window.close();
                                            cb.call(scope);
                                        } else if (obj.success === false) {
                                            Ext.toast(obj.message);
                                        }
                                    },
                                    function (response, opts) {
                                        Ext.toast(`删除失败：${response.status}`);
                                    });
                        }
                    }
                }, {
                    text: '取消',
                    scale: 'medium',
                    border: false,
                    cls: 'btnCancel',
                    handler: function () {
                        this.up('window').close();
                    }
                }]
            }).show();
        },
        //删除操作
        //删除确认操作
        deleteConfirm: function (cb, scope) {
            Ext.create('Ext.window.Window', {
                height: '250',
                width: '450',

                title: '提示',
                border: false, modal: true, constrainHeader: true, buttonAlign: 'center',
                items: [{
                    margin: '30 20 0 20',
                    align: 'center',
                    height: 200,
                    padding: 5,
                    html: '<center><span style="color:#2672d9;font-size:20px;margin-top:5px;font-weight: normal;">是否删除选中的内容?</span><br><br><span style="color:#8d9ebd;font-size:16px;">删除后不可恢复</span></center>',
                    id: 'managerWindowsBody',
                    bodyStyle: {background: '#fff'}
                }],
                dockedItems: [
                    {
                        dock: 'bottom',
                        xtype: 'toolbar',
                        border: false,
                        margin: '-40 -40 0 0 ',
                          style: {
                              background: '#fff'
                          },
                           bodyStyle: {background: '#fff'},
                        id: 'managerWindowsFoot',
                        items: ['->', {
                            text: '删除',
                            cls:'btnSubmit',
                            width: '25%',
                            scale: 'medium',
                            margin:'0 20 40 0',
                            handler: function (btn) {
                                let window = btn.up('window');
                                window.close();
                                cb.call(scope);
                            },
                            scope: this,
                            action_type: 'buildingAdd'
                        },{
                            text: '取消',
                            scale: 'medium',
                            border:false,
                            cls: 'btnCancel',
                            width: '25%',
                            handler: function (btn) {
                                btn.up('window').close();
                            },
                            scope: this,
                            action_type: 'buildingAdd'
                        }, '->']
                    }],
            }).show();
        },

        // 通用combobox，带清除按钮
        combobox: function (store, cfg = {}) {
            let triggers = {
                clearBtn: {
                    hidden: true, cls: 'x-form-clear-trigger', weight: -2,
                    handler: function () {
                        this.clearValue();
                    }
                }
            };
            let config = Ext.Object.mergeIf(cfg, {
                triggers: triggers,
                hideTrigger: true,
                editable: false, queryMode: 'remote',
                displayField: 'name', valueField: 'value'
            });
            // 此处的store必须单独配置，不能放在mergeIf里，否则会加载相同store
            config.store = store;
            let combo = Ext.create('Ext.form.field.ComboBox', config);
            combo.on('change', 'clearComboValue');
            return combo;
        },

        // 通用window
        window: function (item, cfg) {
            let config = Ext.Object.merge({
                title: '',
                modal: true,
                constrainHeader: true,
                scrollable: true,
                resizable: false,
                layout: 'fit',
                // buttons: buttons,
                items: item,
                style: {
                    background: '#768693',
                    border: 'none',
                }
            }, cfg);
            return Ext.create('Ext.window.Window', config);
        },

        // 系统面板添加子panel
        sysPanelAdd: function (tabId, params) {
            //获取父级tab组件
            params = params || {};
            let cmp = Ext.getCmp(params.id || tabId);
            let SysContainer = Ext.getCmp('SysContainer');
            if (!cmp) {
                cmp = Ext.create(tabId, params);
                SysContainer.add(cmp);
            }
            SysContainer.getLayout().setActiveItem(cmp);
        },

        // treeNode冒泡
        cascade: function (obj, callback) {
            Ext.Array.each(obj, function (item) {
                callback(item);
                if (item.children) {
                    util.cascade(item.children, callback);
                }
            });
        },

        // 确认框
        confirm: function (msg, url, params, success, fail, scope) {
            Ext.Msg.confirm('提示', msg, (ok) => {
                if (ok === 'yes') {
                    util.get(url, params, success, fail, scope);
                }
            });
        },
        // 带文本框的确认框
        confirmPrompt(msg, url, params, success, fail) {
            Ext.Msg.prompt('提示', msg, (ok, str) => {
                if (ok === 'ok') {
                    params.denial_reason = str || '';
                    util.get(url, params).then(success, fail);
                }
            }, null, true);
            //title, message, [fn], [scope], [multiline], [value]
        },

        // 返回指定组件的hidden属性值
        hidden: function (name) {
            let sysButton = Ext.userData.sysbutton;
            return !sysButton.includes(name);
        },
        /**
         *@desc 用于测试环境调试输出使用
         *@author LiHongKun
         *@date 2018-10-12 11:17:08
         *@param data:Object
         */
        printLog: function (data) {
            /* return; //部署生产环境时候去掉此行注释
             console.log();*/
            if (typeof data === 'undefined') {
                console.log('Data is not defined.');
                //console.log(val);
            } else if (data === null) {
                console.log('Data is Null.');
            } else {
                let getType = data.constructor;
                console.log('Data type is:' + getType);
                if (getType === String) {
                    var regu = "^[ ]+$";
                    var re = new RegExp(regu);
                    if (data === '' || re.test(data)) {
                        console.log('data is Empty charaters.');
                    }
                    else {
                        console.log(data)
                    }
                }
                else if (getType === Object) {
                    console.log(JSON.stringify(data));
                }
                else if (getType === Array) {
                    for (var key in data) {
                        console.log(key + ':' + data[key]);
                    }
                } else {
                    console.log(data.toString());
                }

            }
        },
        getUserGroup: function () {
            return Ext.util.Cookies.get('cookieGroupId');
        },
        // 返回是否admin
        isAdmin: function () {
            return util.getUserGroup() === '1';
        },

        passwordConfirm: function (cb, scope) {
            let vims = {
                xtype: 'panel',
                bodyPadding: '50 0 0 170',
                defaultType: 'textfield',
                html: '<span style="font-size:24px;">确认初始密码吗?</span>',
                dockedItems: [{
                    dock: 'bottom',
                    xtype: 'toolbar',
                    style:'background:#fff',
                    border:false,
                    height: 60,
                    margin: '-70 0 20 120',
                    items: ['->', {
                        xtype: 'button',
                        cls:'btnSubmit',
                        arrow: true,
                        text: `确定`,
                        width:132,
                        height:42,
                        margin: '0 0 10 0',
                        scale: 'medium',
                        handler: function (btn) {
                            let window = btn.up('window');
                            window.close();
                            cb.call(scope);
                        },
                        scope: this,
                        action_type: 'buildingAdd'
                    }, {
                        xtype: 'button',
                        cls: 'btnCancel',
                        text: `取消`,
                        width:132,
                        height:42,
                        margin: '0 120 10 30',
                        scale: 'medium',
                        handler: function (btn) {
                            btn.up('window').close();
                        },
                    }]
                }]
            };
            let winS2 = util.window(vims, {
                title: '提示',
                height: '250',
                width: '500',
            });
            winS2.show();
        },

    }
});